var express = require('express');
var router = express.Router();
var api = require('../ai');
var fs = require('fs');

var multer = require('multer');

var uploadImage = multer({
  storage: multer.diskStorage({
    destination: 'public/uploads/',
  }),
  fileFilter: function (req, file, callback) {
    if (!file.mimetype.match(/^image\//i)) {
      return callback('只能上传图片文件!', false);
    }
    callback(null, true);
  },
  limits: {
    fileSize: 30 * 1024 * 1024
  }
});

/* GET home page. */
router.post('/idcard', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    var idCardSide = "front";
    // 调用身份证识别
    api.ocr.idcard(image, idCardSide).then(function (result) {
      console.log(JSON.stringify(result));
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

router.post('/business', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    // 调用身份证识别
    api.ocr.businessLicense(image).then(function (result) {
      console.log(JSON.stringify(result));
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

router.post('/business', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    // 调用营业执照
    api.ocr.businessLicense(image).then(function (result) {
      console.log(JSON.stringify(result));
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

router.post('/bankcard', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    // 调用银行卡识别
    api.ocr.bankcard(image).then(function (result) {
      console.log(JSON.stringify(result));
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

router.post('/generalBasic', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    // 调用通用文字识别, 图片参数为本地图片
    api.ocr.generalBasic(image).then(function (result) {
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

router.post('/vehicleLicense', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    api.ocr.vehicleLicense(image).then(function (result) {
    })

  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

router.post('/drivingLicense', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    api.ocr.drivingLicense(image).then(function (result) {
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});


router.post('/licensePlate', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    api.ocr.licensePlate(image, { multi_detect: true }).then(function (result) {
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});



router.post('/vatInvoice', uploadImage.any(), function (req, res, next) {
  if (req.files && req.files.length) {
    let data = req.files.map((file) => {
      return {
        url: `/${file.path}`,
        size: file.size
      }
    });
    var image = fs.readFileSync(`${process.cwd()}/${req.files[0].path}`).toString("base64");
    api.ocr.vatInvoice(image).then(function (result) {
      res.json({
        code: 0,
        msg: '成功',
        data: result
      });
    }).catch(function (err) {
      // 如果发生网络错误
      console.log(err);
      res.json({
        code: 1,
        msg: '发生网络错误'
      });
    });
  } else {
    res.json({
      code: 1,
      msg: '上传出错!'
    })
  }
});

module.exports = router;
